//package com.pug.order.nacos;
//
//import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
//import com.alibaba.cloud.nacos.NacosServiceInstance;
//import com.alibaba.nacos.api.exception.NacosException;
//import com.alibaba.nacos.api.naming.NamingService;
//import com.alibaba.nacos.api.naming.pojo.Instance;
//import com.alibaba.nacos.client.naming.NacosNamingService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.loadbalancer.DefaultResponse;
//import org.springframework.cloud.client.loadbalancer.EmptyResponse;
//import org.springframework.cloud.client.loadbalancer.Request;
//import org.springframework.cloud.client.loadbalancer.Response;
//import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
//import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
//import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//
///**
// * @program: xq_pug_alibabacloud_parent
// * @description:
// * @author: Mr.Ren
// * @create: 2023-04-17 21:20
// **/
//@Slf4j
//public class RgsNacosWeightRoute extends RoundRobinLoadBalancer {
//
//    private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
//
//    private String serviceId;
//
//    @Autowired
//    private NacosDiscoveryProperties nacosDiscoveryProperties;
//
//    public RgsNacosWeightRoute(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, String serviceId) {
//        super(serviceInstanceListSupplierProvider, serviceId);
//        this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
//        this.serviceId = serviceId;
//    }
//
//    @Override
//    public Mono<Response<ServiceInstance>> choose(Request request) {
//        try {
//            ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
//
//            return supplier.get(request).next().map(serviceInstances -> getInstanceResponse(serviceInstances, request));
//        } catch (Exception e) {
//
//        }
//        return super.choose(request);
//    }
//
//
//    private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances, Request request) {
//
//        //1.当前实例为空
//        if (instances.isEmpty()) {
//            if (log.isWarnEnabled()) {
//                log.warn("No servers available for service: " + this.serviceId);
//            }
//
//            return new EmptyResponse();
//        }
//        try {
//
//            NamingService namingService = new NacosNamingService(nacosDiscoveryProperties.getNacosProperties());
//
//
//            Instance instance = namingService.selectOneHealthyInstance(serviceId,nacosDiscoveryProperties.getGroup());
//
//            //对象转化
//            NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
//            nacosServiceInstance.setHost(instance.getIp());
//            nacosServiceInstance.setMetadata(instance.getMetadata());
//            nacosServiceInstance.setPort(instance.getPort());
//            nacosServiceInstance.setServiceId(instance.getServiceName());
//
//            //返回选中的实例
//            return new DefaultResponse(nacosServiceInstance);
//
//        } catch (NacosException e) {
//            log.error("未找到服务实例");
//            return super.choose(request).block();
//        }
//
//    }
//
//}
